package com.easy_buy.service;

import com.easy_buy.domain.Preshop;
import com.easy_buy.mapper.StoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    StoreMapper mapper;

    @Override
    @Transactional
    public void payOrder(String ucode, List<Preshop> preshopList) {
        //确定订单号
        Integer oid;
        if(mapper.getOid() == null){
            oid = 1;
        }else {
            oid = Long.valueOf((Long) mapper.getOid()).intValue();
        }

        //确认购物及更新库存
        for (int i = 0; i < preshopList.size(); i++) {
            if (preshopList.get(i).getConfirm()) {
                mapper.confirmPreshop(ucode, preshopList.get(i).getPid(), preshopList.get(i).getCnum(), oid);
                mapper.updateStocks(preshopList.get(i).getPid(), preshopList.get(i).getCnum());
            }
        }
        //生成订单
        float total = 0;
        for (int i = 0; i < preshopList.size(); i++) {
            if (preshopList.get(i).getConfirm()) {
                total += preshopList.get(i).getPprice() * preshopList.get(i).getCnum();
            }
        }
        mapper.insertOrder(oid, ucode, total);
    }
}
